{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xml.etree.ElementTree as ET\n",
    "from tqdm import tqdm\n",
    "keys = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "clean = [' ', '、', '(', ')', '台灣', ':']\n",
    "def clean_txt(input):\n",
    "    for c in clean:\n",
    "        input = input.replace(c, '')\n",
    "    return input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "14it [00:16,  1.15s/it]\n"
     ]
    }
   ],
   "source": [
    "for i, idx in tqdm(enumerate(range(14), start=97)):\n",
    "    tree = ET.parse(f'./GRB_{i}.xml')\n",
    "    root = tree.getroot()\n",
    "    for grb05 in root.findall('GRB05'):\n",
    "        g = grb05.find('KEYWORD_C')\n",
    "        if g != None:\n",
    "            if '；' in g.text:                \n",
    "                temp = list(set(g.text.split('；')))                \n",
    "            if ';' in g.text:\n",
    "                temp = list(set(g.text.split('；')))\n",
    "            temp = [clean_txt(w) for w in temp if len(clean_txt(w)) < 5]\n",
    "            keys.extend(temp)                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "159076"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keys = list(filter(None, keys))\n",
    "keys = list(set(keys))\n",
    "keys = [clean_txt(k) for k in keys]\n",
    "len(keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "南韓\n"
     ]
    }
   ],
   "source": [
    "print(keys[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('keys.txt', 'w') as f:\n",
    "    for item in keys:\n",
    "        f.write(\"%s\\n\" % item)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aa781dd5d8b0b47d7fc97d3d29d31ddde4cdca0824a34701095d27847d24a55d"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
